package com.hyopt.modules.app.controller;


import com.hyopt.common.exception.MyException;
import com.hyopt.common.utils.R;
import com.hyopt.modules.app.entity.UserEntity;
import com.hyopt.modules.app.form.LoginForm;
import com.hyopt.modules.app.service.UserService;
import com.hyopt.modules.app.utils.JwtUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;


/**
 * TODO app登录注册的相关操作
 *
 * @author crazypenguin
 * @version 1.0.0
 * @createdate 2019/1/3
 */
@RestController
@RequestMapping("/app")
@Api(value = "AppLoginController", tags = {"APP登录注册相关接口"})
public class AppLoginController {

    @Resource
    private UserService userService;
    /**
     * 登陆
     * @param form
     * @return
     */
    @PostMapping("/login")
    @ApiOperation("登陆")
    public R login(@RequestBody @Validated LoginForm form) {
        try {
            UserEntity checking = userService.  checking(form);
            return R.ok().put("data",checking);
        }catch (MyException e){
            return R.error(e.getMsg());
        }
    }


    @PostMapping("/logout/{id}")
    @ApiOperation("登出")
    public R logout(@PathVariable("id") Integer id) {
        try {
            userService.logout(id);
            return R.ok();
        }catch (MyException e){
            return R.error(e.getMsg());
        }
    }

}
